package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.BigWheel;
import com.wangsd.web.coupon.entity.NewGift;
import com.wangsd.web.coupon.entity.NewGiftRelation;
import com.wangsd.web.coupon.entity.Product;
import com.wangsd.web.coupon.mapper.NewGiftMapper;
import com.wangsd.web.coupon.mapper.NewGiftRelationMapper;
import com.wangsd.web.coupon.service.NewGiftRelationService;
import com.wangsd.web.coupon.service.NewGiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-22
 */
@Service
public class NewGiftServiceImpl extends ServiceImpl<NewGiftMapper, NewGift> implements NewGiftService {

    @Resource
    NewGiftMapper newGiftMapper;

    @Resource
    NewGiftRelationMapper newGiftRelationMapper;

    @Autowired
    NewGiftRelationService newGiftRelationService;

    @Override
    public IPage<NewGift> selectPage(String code, Integer pageNum, Integer pageSize, NewGift newGift) {
        Page<NewGift> page = new Page<>(pageNum, pageSize);
        return newGiftMapper.selectListXml(page, code, newGift);
    }

    @Override
    public int addOrUpdate(NewGift entity) {
        if (entity.getId() == null) {
            newGiftMapper.insert(entity);
            List<NewGiftRelation> productRelations = new ArrayList<>();

            /*保存关联活动和商品的关联关系*/
            entity.getProductIds().forEach(productId -> {
                NewGiftRelation newGiftRelation = new NewGiftRelation();
                newGiftRelation.setNewGiftId(entity.getId());
                newGiftRelation.setObjectId(productId);
                productRelations.add(newGiftRelation);
            });
            newGiftRelationService.saveBatch(productRelations);
        } else {
            /*修改*/
            newGiftMapper.updateById(entity);
            /*删除之前活动和商品关联关系数据*/
            newGiftRelationMapper.delete(new QueryWrapper<NewGiftRelation>().eq("new_gift_id", entity.getId()));

            /*更新新的关联关系*/
            List<NewGiftRelation> productRelations = new ArrayList<>();
            entity.getProductIds().forEach(productId -> {
                NewGiftRelation newGiftRelation = new NewGiftRelation();
                newGiftRelation.setObjectId(productId);
                newGiftRelation.setNewGiftId(entity.getId());
                productRelations.add(newGiftRelation);
            });
            newGiftRelationService.saveBatch(productRelations);
        }
        return 1;
    }

    @Override
    public int upActivity(NewGift newGift) {
        /*上架参加当前活动商品时，需将该商圈下的其他活动置为下架状态*/
        if (newGift.getStatus() == 1) {
            NewGift byId = newGiftMapper.selectById(newGift.getId());
            Long companyId = byId.getCompanyId();
            newGiftMapper.updateStatusByCompanyId(companyId, 0);
        }
        /*更新当前商品状态为状态*/
        this.updateStatus(newGift.getId(), newGift.getStatus());
        return 1;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        NewGift newGift = new NewGift();
        newGift.setStatus(status);
        newGift.setId(id);
        newGiftMapper.updateById(newGift);
        return 0;
    }

    @Override
    public IPage<NewGift> selectListByCompanyId(Integer pageNum, Integer pageSize, Long companyId) {
        NewGift newGift = new NewGift();
        newGift.setCompanyId(companyId);
        Page<NewGift> page = new Page<>(pageNum, pageSize);
        return newGiftMapper.selectListByCompanyIdXml(page, newGift);
    }

    @Override
    public List<String> selectNewGiftByCompanyId(Long id) {
        NewGift newGift = new NewGift();
        newGift.setId(id);
        List<String> collect = null;
        List<NewGiftRelation> list = newGiftRelationMapper.selectNewGiftProductByCompanyId(newGift);
        if (list != null && list.size() > 0) {
            collect = list.stream().map(NewGiftRelation::getObjectId).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public List<String> selectNewGiftProductByCompanyId(Long companyId, Integer activityType) {
        NewGift newGift = new NewGift();
        newGift.setCompanyId(companyId);
        if(activityType!=null&&activityType==2){
            newGift.setBeginTime(new Date());
        }
        List<NewGiftRelation> list = newGiftRelationMapper.selectNewGiftProductByCompanyId(newGift);
        List<String> collect = null;
        if (list != null && list.size() > 0) {
            collect = list.stream().map(NewGiftRelation::getObjectId).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public NewGift selectNewGiftByProductId(String objectId, Integer objectType) {
        NewGift newGift = new NewGift();
        newGift.setObjectId(objectId);
        newGift.setObjectType(objectType);
        List<NewGift> newGifts = newGiftMapper.selectListByCompanyId(newGift);
        if (newGifts != null && newGifts.size() > 0) {
            return newGifts.get(0);
        } else {
            return null;
        }
    }
}
